package com.ukao.pad.adapter;

import android.content.Context;
import android.support.annotation.ColorRes;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mcxtzhang.commonadapter.rv.CommonAdapter;
import com.mcxtzhang.commonadapter.rv.ViewHolder;
import com.ukao.pad.R;
import com.ukao.pad.bean.StockinBean;
import com.ukao.pad.bean.StringBean;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.listener.OnItemChildClickListener;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.DecimalUtil;
import com.ukao.pad.widget.StateButton;


import java.util.List;

import static com.ukao.pad.R.id.phone_laout;

/**
 * Created by ukaodev on 2017/6/14.
 */


public class StockinAdapter extends CommonAdapter<StockinBean.ProListBean> {

    private OnItemChildClickListener mOnItemChildClickListener;

    private  boolean isShowMarkBtn=false;
    public  void  setOnItemChildClickListener(OnItemChildClickListener  mOnItemChildClickListener){
        this.mOnItemChildClickListener=mOnItemChildClickListener;
    }




    public StockinAdapter(Context context, List<StockinBean.ProListBean> datas  ) {
        super(context, datas, R.layout.stockin_list_item);

    }


    public  void   setShowMarkBtn(boolean  isShowMarkBtn){
        this.isShowMarkBtn=isShowMarkBtn;


    }
    private  int proTotala;
    public  void  setProTotal(int proTotala){
            this.proTotala=proTotala;
    }

    @Override
    public void convert(final ViewHolder holder, StockinBean.ProListBean dataBean) {

        //拍照layout
        RelativeLayout phoneLaout = holder.getView(R.id.phone_laout);
        TextView productName = holder.getView(R.id.productName);
        TextView serviceName = holder.getView(R.id.serviceName);
        TextView businessName = holder.getView(R.id.businessName);
        TextView serviceAmt = holder.getView(R.id.serviceAmt);
        TextView addsrvAmt = holder.getView(R.id.addsrvAmt);
        TextView subtotal = holder.getView(R.id.subtotal);
        TextView sortNo = holder.getView(R.id.sortNo);
        TextView annexDesc = holder.getView(R.id.annexDesc);
        TextView addServiceDesc = holder.getView(R.id.addServiceDesc);
        TextView colorDesc = holder.getView(R.id.colorDesc);
        TextView flawDesc= holder.getView(R.id.flawDesc);
        TextView  effectDesc = holder.getView(R.id. effectDesc);
        TextView  brandName = holder.getView(R.id. brandName);
        TextView  saleAmt = holder.getView(R.id. saleAmt);
        TextView  picCnt = holder.getView(R.id.picCnt);
        TextView  total = holder.getView(R.id.stockin_list_item_total);
        TextView  deleteBt = holder.getView(R.id.stockin_list_item_delete_bt);
        Button copyBt = holder.getView(R.id.stockin_list_item_copy_bt);
        TextView  chgSubtotal = holder.getView(R.id.chgSubtotal);
        Button markingBtn= holder.getView(R.id.stockin_list_item_marking_bt);
        //拍照button
        Button photoBtn= holder.getView(R.id.stockin_list_item_photo_bt);
        StateButton bingCodeBtn= holder.getView(R.id.bing_code_btn);
        TextView  bingCode = holder.getView(R.id.bing_code);
        ImageView accessory = holder.getView(R.id.accessory);

        //如果是商米设备屏蔽
//        if (CheckUtils.isMIUIDevices()){
//            phoneLaout.setVisibility(View.GONE);
//        }else {
            phoneLaout.setVisibility(dataBean.isClothing()?View.VISIBLE:View.INVISIBLE);
//        }

        copyBt.setVisibility(dataBean.isClothing()?View.VISIBLE:View.INVISIBLE);
        accessory.setVisibility(dataBean.isClothing()?View.GONE:View.VISIBLE);
        bingCode.setText(dataBean.getBindCode());
        if (CheckUtils.isEmpty(dataBean.getBindCode())){
            bingCodeBtn.setText(R.string.tie_the);
            bingCodeBtn.setNormalBackgroundColor(getColors(R.color.blue));
            bingCodeBtn.setPressedBackgroundColor(getColors(R.color.default_blue));
        }else{
            bingCodeBtn.setText(R.string.has_been_tied);
            bingCodeBtn.setNormalBackgroundColor(getColors(R.color.green));
            bingCodeBtn.setPressedBackgroundColor(getColors(R.color.perssed_green));
        }
        total.setText(getDatas().size()+"");
        businessName.setText(CheckUtils.isEmptyString(dataBean.getBusinessName())); /* 项目  */
        productName.setText(CheckUtils.isEmptyString(dataBean.getProductName())); /* 产品名称  */
        serviceName.setText(CheckUtils.isEmptyString(dataBean.getServiceName())); /* 服务  */

        String name = !CheckUtils.isEmpty(dataBean.getServiceName()) ? dataBean.getServiceName() + "  : " : "";
        serviceAmt.setText(name+ DecimalUtil.multiply(CheckUtils.isEmptyNumber(dataBean.getServiceAmt()),CheckUtils.isEmptyNumber(dataBean.getCount()))); /* 服务金额  */
        saleAmt.setText( "折后："+""+CheckUtils.isEmptyNumber(dataBean.getSaleAmt())); /* 折后金额  */
        addsrvAmt.setText("附加：" + CheckUtils.isEmptyNumber(dataBean.getAddsrvAmt())); /* 附加金额  */
        subtotal.setText( CheckUtils.isEmptyNumber(dataBean.getSubtotal())+""); /* 小计  */
        sortNo.setText(CheckUtils.isNull(dataBean.getNo())?"":dataBean.getNo()+""); /* 序号  */
        if (!CheckUtils.isNull(dataBean.getPicCnt())&&dataBean.getPicCnt()!=0){
             picCnt.setText(dataBean.getPicCnt()+"");
              picCnt.setVisibility(View.VISIBLE);/*图片数量*/
        }else{
            picCnt.setVisibility(View.GONE);
        }
        if(!CheckUtils.isNull(dataBean.getChgSubtotal())){
            chgSubtotal.setVisibility(View.VISIBLE);
            chgSubtotal.setText(CheckUtils.isEmptyNumber(dataBean.getChgSubtotal()));
        }else{
            chgSubtotal.setVisibility(View.GONE);
        }
        if (isShowMarkBtn){
            if ( SaveParamets.isRfidLabel()){
                markingBtn.setVisibility(View.GONE);
                bingCodeBtn.setVisibility( View.VISIBLE);
            }else{
                markingBtn.setVisibility(View.VISIBLE);
                bingCodeBtn.setVisibility( View.GONE);
            }
        }else{
            bingCodeBtn.setVisibility(View.GONE);
            markingBtn.setVisibility(View.GONE);

        }



                Gson   mGson=new Gson();
                if (!CheckUtils.isEmpty(dataBean.getAddServiceDesc())){
                    List<StringBean>        mServiceDesclist = mGson.fromJson(dataBean.getAddServiceDesc(),
                            new TypeToken<List<StringBean>>() {
                            }.getType());
                    String service = "";
                    for (StringBean item : mServiceDesclist) {
                        service = service + item.getName() + CheckUtils.isEmptyNumber(item.getAmt())+"元";
                    }

                        addServiceDesc.setText(service); /* 附加  */
                }else{
                    addServiceDesc.setText(""); /* 附加  */
                }


        int annexDescCount=0;
        String  annex="";
        if (!CheckUtils.isEmpty(dataBean.getAnnexDesc())){  /*附件*/
            List<StringBean>    mAnnexDesclist= mGson.fromJson(dataBean.getAnnexDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mStringBuffer3 = new StringBuffer();
            for (int i = 0; i < mAnnexDesclist.size(); i++) {
                annexDescCount=annexDescCount+1;
                if (i != mAnnexDesclist.size() - 1) {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName() + "/");
                } else {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName());
                }


            }
            annex = mStringBuffer3.toString();

        }
        if (annexDescCount!=0){
            annexDesc.setText(annexDescCount+"  "+annex);
        }else{
            annexDesc.setText("");
        }


        if (!CheckUtils.isEmpty(dataBean.getColorDesc())){
            List<StringBean>        mAnnexDesclist = mGson.fromJson(dataBean.getColorDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mColorBuffer = new StringBuffer();
            for (int i = 0; i < mAnnexDesclist.size(); i++) {
                annexDescCount = annexDescCount + 1;
                if (i != mAnnexDesclist.size() - 1) {
                    mColorBuffer.append(mAnnexDesclist.get(i).getName() + "/");
                } else {
                    mColorBuffer.append(mAnnexDesclist.get(i).getName());
                }


            }
            colorDesc.setText(mColorBuffer.toString());
        }else{
            colorDesc.setText("");


        }
        if (!CheckUtils.isEmpty(dataBean.getFlawDesc())){
            List<StringBean>        mAnnexDesclist = mGson.fromJson(dataBean.getFlawDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mflawDescBuffer = new StringBuffer();
            for (int i = 0; i < mAnnexDesclist.size(); i++) {
                annexDescCount = annexDescCount + 1;
                if (i != mAnnexDesclist.size() - 1) {
                    mflawDescBuffer.append(mAnnexDesclist.get(i).getName() + "/");
                } else {
                    mflawDescBuffer.append(mAnnexDesclist.get(i).getName());
                }


            }

            flawDesc.setText(mflawDescBuffer.toString()); /*瑕疵*/
        }else{
            flawDesc.setText("");


        }
        if (!CheckUtils.isEmpty(dataBean.getEffectDesc())){
            List<StringBean>        mAnnexDesclist = mGson.fromJson(dataBean.getEffectDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());
            StringBuffer mReffectDescBuffer = new StringBuffer();
            for (int i = 0; i < mAnnexDesclist.size(); i++) {
                annexDescCount = annexDescCount + 1;
                if (i != mAnnexDesclist.size() - 1) {
                    mReffectDescBuffer.append(mAnnexDesclist.get(i).getName() + "/");
                } else {
                    mReffectDescBuffer.append(mAnnexDesclist.get(i).getName());
                }


            }

            effectDesc.setText(mReffectDescBuffer.toString()); /*洗后效果*/
        }else{
            effectDesc.setText("");


        }



        brandName.setText(CheckUtils.isEmptyString(dataBean.getBrandName()));/*品牌*/
           View.OnClickListener  OnClickListener= new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    mOnItemChildClickListener.onChildItemClick(v.getId(), getPosition(holder));
                }catch (Exception e){

                }


            }
        };
        photoBtn.setOnClickListener(OnClickListener);
        deleteBt.setOnClickListener(OnClickListener);
        copyBt.setOnClickListener(OnClickListener);
        markingBtn.setOnClickListener(OnClickListener);
        bingCodeBtn.setOnClickListener(OnClickListener);
    }
    public  int  getColors( @ColorRes int id){
        return   ContextCompat.getColor(mContext,id);

    }
}


